<template>
  <div class="wrap">
    <div class="card-table">
      <TableComponent :columns="shareTypeColumns" :data="tableData" :tableOption="tableOption" :stripe="stripe">
          <template scope="scope">
            <el-button @click="editRow(scope.scope.row)" type="text" size="mini">编辑</el-button>
          </template>
      </TableComponent>
    </div>
    <div class="add-form">
      <el-button type="text" @click="handleAdd">新增</el-button>
    </div>
    <el-dialog :title="title" :visible.sync="dialogTableVisible" width="60%" :before-close="handleCancle">
      <el-form ref="form" :model="shareTypeForm" label-width="125px" label-position="left" style="padding: 0 30px;" :rules="rules">
        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item label="股份类别名称" prop="shareCategoryName" style="margin-bottom: 12px;">
              <el-input v-model="shareTypeForm.shareCategoryName" size="medium" clearable />
            </el-form-item>
            <el-form-item label="交易类型" prop="transactionType" style="margin-bottom: 12px;">
              <el-select v-model="shareTypeForm.transactionType" placeholder="请选择" size="medium" clearable class="select" @change="transactionTypeChange">
                <el-option
                  v-for="item in transactionTypeOptions"
                  :key="item.index"
                  :label="item.value"
                  :disabled="item.isValid==='1'?false:true"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-show="transaction" label="上市交易所及板块" prop="listedExchange" style="margin-bottom: 12px;">
              <el-select v-model="shareTypeForm.listedExchange" placeholder="请选择" size="medium" class="select" clearable>
                <el-option
                  v-for="item in listedExchangeOptions"
                  :key="item.index"
                  :label="item.value"
                  :disabled="item.isValid==='1'?false:true"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-show="transaction" label="上市(挂牌)时间" prop="startDate" style="margin-bottom: 12px;">
              <el-date-picker type="date" v-model="shareTypeForm.startDate" size="medium" clearable format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
            <el-form-item label="每股面值(原币)" prop="priceSharePerOc" style="margin-bottom: 12px;">
              <el-input v-model="shareTypeForm.priceSharePerOc" size="medium" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="股份类型" prop="shareType" style="margin-bottom: 12px;">
              <el-select v-model="shareTypeForm.shareType" placeholder="请选择" size="medium" class="select" clearable>
                <el-option
                  v-for="item in shareTypeOption"
                  :key="item.index"
                  :label="item.value"
                  :disabled="item.isValid==='1'?false:true"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="交易币种" prop="currency" style="margin-bottom: 12px;">
              <el-select v-model="shareTypeForm.currency" placeholder="请选择" size="medium" class="select" clearable>
                <el-option
                  v-for="item in currencyOptions"
                  :key="item.index"
                  :label="item.description"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-show="transaction" label="股票代码" prop="shareCode" style="margin-bottom: 12px;">
              <el-input v-model="shareTypeForm.shareCode" size="medium" clearable />
            </el-form-item>
            <el-form-item v-show="transaction" label="股票简称" prop="shareShortName" style="margin-bottom: 12px;">
              <el-input v-model="shareTypeForm.shareShortName" size="medium" clearable />
            </el-form-item>
            <el-form-item label="是否有面值" prop="isValue" style="margin-bottom: 12px;">
              <el-select v-model="shareTypeForm.isValue" placeholder="请选择" size="medium" class="select" clearable>
                <el-option
                  v-for="item in options"
                  :key="item.index"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TableComponent from '@/components/Table/table'
import { shareTypeColumns } from '../js/index.js'
import { queryShareType, companyShareList, editShareTypeFormal, moneyType, selectShareType, selectTransactType, selectListedExchange } from '@/api/modules/propertyRight.js'
import { checkNumber } from '@/utils/pattern.js'
export default {
  components: {
    TableComponent
  },
  data() {
    return {
      shareTypeColumns,
      tableData: [],
      tableOption: {
        isShowOperate: true,
        operateWidth: '100px'
      },
      stripe: true,
      options: [{
        value: '1',
        label: '是'
      }, {
        value: '0',
        label: '否'
      }],
      currencyOptions: [],
      // dialog=================
      dialogTableVisible: false,
      shareTypeForm: {
        cmhkId: '',
        shareCategoryName: '',
        transactionType: '',
        priceSharePerOc: null,
        shareType: '',
        currency: '',
        isValue: '',
        listedExchange: '',
        startDate: '',
        shareCode: '',
        shareShortName: ''
      },
      title: '',
      shareTypeOption: [],
      transactionTypeOptions: [],
      listedExchangeOptions: [],
      rules: {
        priceSharePerOc: [{ validator: checkNumber, trigger: 'change' }]
      },
      saveCmhkId: {
        cmhkId: ''
      },
      transaction: false
    }
  },
  methods: {
    // 股份类别查询(该方法在父组件中调用)
    getShareType(params) {
      this.saveCmhkId.cmhkId = params.cmhkId
      this.shareTypeForm.cmhkId = params.cmhkId
      companyShareList(params).then(res => {
        if (res.data.code === '0') {
          this.tableData = res.data.body
        }
      })
    },
    // 确定新增/修改股东类别
    handleSubmit() {
      if (this.title === '新增') {
        editShareTypeFormal(this.shareTypeForm).then(res => {
          if (res.data.code === '0') {
            this.$message.success('新增成功')
            this.resetForm('form')
            this.dialogTableVisible = false
            this.getShareType(this.saveCmhkId)
          }
        })
      } else if (this.title === '修改') {
        editShareTypeFormal(this.shareTypeForm).then(res => {
          if (res.data.code === '0') {
            this.resetForm('form')
            this.dialogTableVisible = false
            this.getShareType(this.saveCmhkId) // 修改成功后重新查询
            this.$message.success('修改成功')
          }
        })
      }
    },
    // 修改股东类别
    editRow(row) {
      console.log(row)
      this.dialogTableVisible = true
      this.title = '修改'
      const params = {
        shareCategoryName: row.shareCategoryName,
        cmhkId: row.cmhkId
      }
      queryShareType(params).then(res => {
        if (res.data.code === '0') {
          this.shareTypeForm = res.data.body
          if (res.data.body.transactionType === '03') {
            this.transaction = true
          } else {
            this.transaction = false
          }
        }
      })
      moneyType().then(res => {
        if (res.data.code === '0') {
          this.currencyOptions = res.data.body
        }
      })
      selectShareType().then(res => {
        if (res.data.code === '0') {
          this.shareTypeOption = res.data.body
        }
      })
      selectTransactType().then(res => {
        if (res.data.code === '0') {
          this.transactionTypeOptions = res.data.body
        }
      })
      selectListedExchange().then(res => {
        if (res.data.code === '0') {
          this.listedExchangeOptions = res.data.body
        }
      })
    },
    // 点击新增
    handleAdd() {
      this.dialogTableVisible = true
      this.title = '新增'
      this.transaction = false
      moneyType().then(res => {
        if (res.data.code === '0') {
          this.currencyOptions = res.data.body
        }
      })
      selectShareType().then(res => {
        if (res.data.code === '0') {
          this.shareTypeOption = res.data.body
        }
      })
      selectTransactType().then(res => {
        if (res.data.code === '0') {
          this.transactionTypeOptions = res.data.body
        }
      })
      selectListedExchange().then(res => {
        if (res.data.code === '0') {
          this.listedExchangeOptions = res.data.body
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleCancle() {
      this.resetForm('form')
      this.dialogTableVisible = false
    },
    // 根基交易类型不同控制显隐
    transactionTypeChange(val) {
      console.log(val)
      if (val === '03') {
        this.transaction = true
      } else {
        this.transaction = false
        this.shareTypeForm.listedExchange = ''
        this.shareTypeForm.startDate = ''
        this.shareTypeForm.shareCode = ''
        this.shareTypeForm.shareShortName = ''
      }

    }
  }
}
</script>

<style lang="less" scoped>
  .wrap {
    .card-table {
      width: 100%;
    }
    .add-form {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 10px;
    }
    .select {
      width: 100%;
    }
  }
</style>
